using UnityEngine;

/// <summary>
/// 阳光系统集成示例 - 展示如何在其他地方使用阳光系统
/// </summary>
public class SunSystemIntegrationExample : MonoBehaviour
{
    [Header("阳光系统控制")]
    [SerializeField] private bool m_EnableRandomSun = true;
    [SerializeField] private KeyCode m_ToggleKey = KeyCode.Space;
    [SerializeField] private KeyCode m_SpawnKey = KeyCode.Return;
    
    private SunSystem m_SunSystem;
    private GameApp m_GameApp;
    
    private void Start()
    {
        // 方法1: 通过服务管理器获取
        m_SunSystem = ServiceManager.Instance.GetService<SunSystem>();
        
        // 方法2: 通过GameApp获取（推荐）
        m_GameApp = FindObjectOfType<GameApp>();
        
        if (m_SunSystem == null)
        {
            Logger.LogError("无法获取SunSystem服务！");
            return;
        }
        
        // 设置初始状态
        m_SunSystem.EnableRandomSun = m_EnableRandomSun;
        
        Logger.Log("阳光系统集成示例初始化完成");
        Logger.Log($"按 {m_ToggleKey} 切换随机阳光生成");
        Logger.Log($"按 {m_SpawnKey} 立即生成阳光");
    }
    
    private void Update()
    {
        if (m_SunSystem == null) return;
        
        // 切换随机阳光生成
        if (Input.GetKeyDown(m_ToggleKey))
        {
            ToggleRandomSun();
        }
        
        // 立即生成阳光
        if (Input.GetKeyDown(m_SpawnKey))
        {
            SpawnRandomSun();
        }
    }
    
    /// <summary>
    /// 切换随机阳光生成
    /// </summary>
    private void ToggleRandomSun()
    {
        m_EnableRandomSun = !m_EnableRandomSun;
        
        // 方法1: 直接控制SunSystem
        m_SunSystem.EnableRandomSun = m_EnableRandomSun;
        
        // 方法2: 通过GameApp控制（推荐）
        if (m_GameApp != null)
        {
            m_GameApp.SetRandomSunEnabled(m_EnableRandomSun);
        }
        
        Logger.Log($"随机阳光生成: {(m_EnableRandomSun ? "开启" : "关闭")}");
    }
    
    /// <summary>
    /// 立即生成阳光
    /// </summary>
    private void SpawnRandomSun()
    {
        // 方法1: 直接调用SunSystem
        m_SunSystem.SpawnRandomSunImmediately();
        
        // 方法2: 通过GameApp调用（推荐）
        if (m_GameApp != null)
        {
            m_GameApp.SpawnRandomSun();
        }
        
        Logger.Log("立即生成随机阳光");
    }
    
    /// <summary>
    /// 设置自定义配置
    /// </summary>
    public void SetCustomConfig()
    {
        if (m_SunSystem == null) return;
        
        // 创建自定义配置
        var customConfig = ScriptableObject.CreateInstance<SunSpawnConfig>();
        customConfig.MinInterval = 3f;
        customConfig.MaxInterval = 8f;
        customConfig.MinSunValue = 20;
        customConfig.MaxSunValue = 30;
        customConfig.SpawnAreaWidth = 25f;
        customConfig.SpawnAreaDepth = 12f;
        customConfig.SpawnHeight = 18f;
        customConfig.FallDuration = 1.5f;
        
        // 应用配置
        m_SunSystem.SetRandomSunConfig(customConfig);
        
        Logger.Log("已应用自定义阳光配置");
    }
    
    /// <summary>
    /// 从资源加载配置
    /// </summary>
    public void LoadConfigFromResources()
    {
        if (m_SunSystem == null) return;
        
        // 从Resources文件夹加载配置
        var config = Resources.Load<SunSpawnConfig>("Datas/SunSpawnConfig");
        if (config != null)
        {
            m_SunSystem.SetRandomSunConfig(config);
            Logger.Log("已加载资源中的阳光配置");
        }
        else
        {
            Logger.LogWarning("未找到阳光配置文件");
        }
    }
}
